/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.gwt.widgets;

import java.util.ArrayList;
import java.util.HashMap;
import com.google.gwt.user.client.ui.*;

public class AccountAutoSuggest extends PopupPanel {

	private int maxItems;
	private ArrayList suggestions = new ArrayList();
	private String needle;
	private HoverTable suggestionTable;
	private SuggestionListener listener;
	private String highLightStyle = "";
	private int cellIdx;
	
	public AccountAutoSuggest(int maxItems) {
		super();
		needle = "";
		suggestionTable = new HoverTable("account_text_suggest_normal","account_text_suggest_highlight");
		this.maxItems = maxItems;
		add(suggestionTable);
		
		suggestionTable.addTableListener(new TableListener() {
			public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
				if(listener != null) {
					listener.onSuggestionClicked(suggestionTable.getText(row, cell));
				}
			}
		});
	}
	
	public void setVisible(boolean visible) {
		super.setVisible(visible);
	}
	
	public void setTableStyle(String style) {
		suggestionTable.setStyleName(style);
	}
	
	public void setHighlightStyle(String style) {
		highLightStyle = style;
	}
	
	public void setSuggestions(ArrayList suggestions) {
		this.suggestions = suggestions;
	}
	
	public void addSuggestion(String suggestion) {
		suggestions.add(suggestion);
	}
	
	public void addSuggestion(String suggestion, String alternate) {
		suggestions.add(suggestion);
	}
	
	public void clearSuggestions() {
		suggestions.clear();
	}
	
	public void setSuggestionListener(SuggestionListener l) {
		listener = l;
	}
	
	private void clearSuggestionTable() {
		
		suggestionTable.clear();
		int len = suggestionTable.getRowCount();
		for(int i = 0; i < len; i++)
			suggestionTable.removeRow(0);
		
		cellIdx = 0;
		
	}
	
	public void hide() {
		setVisible(false);
		clearSuggestionTable();
		super.hide();
	}
	
	public void scrollDown() {
		
		cellIdx++;
		if(cellIdx >= suggestionTable.getRowCount())
			cellIdx = 0;
		suggestionTable.highLight(cellIdx);
	}
	
	public void scrollUp() {
		cellIdx--;
		if(cellIdx < 0)
			cellIdx = suggestionTable.getRowCount() - 1;
		suggestionTable.highLight(cellIdx);
	}
	
	public String getCurrentSuggestion() {
		if(cellIdx > -1 && cellIdx < suggestionTable.getRowCount()) {
			return suggestionTable.getText(cellIdx, 0);
		}
		return "";
	}
	
	public void textChanged(String text) {
		
		//nothing has really changed, so don't do anything
		if(needle.equals(text))
			return;
		
		if(text.length() < 1)
			hide();
		
		if(needle.length() < 1)
			show();
		
		this.needle = text;
		
		int size = suggestions.size();
		int minLen = needle.length(); //minimum word length for an item to be a suggestion candidate
		
		int endIdx = 0; //end of where we will compare two strings
		int suggestionIdx = 0; //how many matches we currently have
		
		System.out.println("size: " + size + " minlen: " + minLen);
		
		ArrayList newSuggestions = new ArrayList();
		for(int i = 0; i < size; i++) {
			String haystack = (String)suggestions.get(i);
			
			if(haystack.length() < minLen)
				continue;
			
			endIdx = (haystack.length() > minLen)?minLen:haystack.length();

			//if both strings start with the same text, this should
			//be added to the suggestion box
			System.out.println("n:"+needle);
			if(needle.equalsIgnoreCase(haystack.substring(0, endIdx))) {
				System.out.println("Adding: " + haystack);
				newSuggestions.add(haystack);
				
				
				//let the listener know of our "first choice" this time
				if(suggestionIdx == 0 && listener != null)
					listener.onSuggestionChanged(haystack);
				
				suggestionIdx++;
			}
			
			//if there's a limit on how many suggestions we show,
			//now's the time to check and break out of the loop
			//if we've reached it
			if(maxItems > 0 && suggestionIdx >= maxItems)
				break;
		}
		
		if(newSuggestions.size() < suggestionTable.getRowCount())
			clearSuggestionTable();
		
		for(int i = 0; i < newSuggestions.size(); i++) {
			System.out.println("Adding: " + (String)newSuggestions.get(i));
			suggestionTable.setText(i, 0, (String)newSuggestions.get(i));
		}
		
		cellIdx = 0;
		suggestionTable.highLight(cellIdx);
	}
	
}
